package com.coocaa.ops.api.base.third.dmp.feign;

import feign.*;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.codec.StringDecoder;
import feign.hystrix.FallbackFactory;
import feign.hystrix.HystrixFeign;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.cloud.openfeign.support.SpringMvcContract;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @author tangminghong
 * @date 2020-09-04
 */
@Configuration
@Import(FeignClientsConfiguration.class)
public class DmpFeignConfig {

    private static final int CONNECT_TIMEOUT_MILLIS = 15000;

    private static final int READ_TIMEOUT_MILLIS = 9000;

    @Bean
    public DmpFeignClientService dmpFeignClientService(Encoder encoder, Decoder decoder, Client client, DmpFeignFallbackFactory fallbackFactory) {
        DmpFeignClient dmpFeignClient = HystrixFeign.builder()
                .encoder(encoder)
                .decoder(decoder)
                .contract(new SpringMvcContract())
                .options(new Request.Options(CONNECT_TIMEOUT_MILLIS, READ_TIMEOUT_MILLIS))
                .retryer(Retryer.NEVER_RETRY)
                .logLevel(Logger.Level.FULL)
                .client(client)
                .target(Target.EmptyTarget.create(DmpFeignClient.class), fallbackFactory);
        DmpFeignClientService clientService = new DmpFeignClientService();
        clientService.setFeignClient(dmpFeignClient);
        return clientService;

    }
}
